<?php

use Upfor\UpforPHP\Http\Url;

/**
 * 路由定义
 *
 * WARNING: 路由的匹配&执行系有顺序的, 请注意调整路由的顺序
 */

// API
// 仅用作对外系统API, 系统内部API请用MVC路由
$app->post('(/api/@controller:[a-zA-Z]+[\w]*)[/]?', function ($params) use ($app) {
    $controller = $params['controller'];
    $method = 'index';
    request()->setController($controller);
    request()->setAction($method);

    $class = '\\SinoPHP\\BbdBoss\\Api\\' . $controller;
    if (!class_exists($class) || !method_exists($class, $method)) {
        throw new \SinoPHP\BbdBoss\Exception\ApiNotFound();
    }

    (new $class($this))->$method();
});

// MVC
// 该路由用于PAGE和系统内API
$app->any('(/@controller:[a-zA-Z]+[\w]*(/@action:[a-zA-Z]+[\w]*)).*', function ($params) use ($app) {
    Url::parseUrl(request()->getPathInfo(), true);

    // 过滤浏览器自动请求ico图标问题
    if (strtolower(request()->getRequestUri()) == '/favicon.ico') {
        exit;
    }

    $controller = request()->getController();
    $method = request()->getAction();
    $class = '\\SinoPHP\\BbdBoss\\Controller\\' . $controller;
    if (!class_exists($class) || !method_exists($class, $method)) {
        throw new \Upfor\UpforPHP\Exception\PageNotFound();
    }

    (new $class($this))->$method();
});
